<template>
  <div class="apply">
    <div class="d-flex justify-content-between">
      <div>
        <div class="leftBar d-flex">
          <el-form :inline="true" :model="searchForm" class="demo-form-inline">
            <el-form-item label="院系">
              <el-select v-model="searchForm.collegeName" placeholder="请选择院系">
                <el-option
                    v-for="item in collegeData"
                    :key="item.key"
                    :label="item.value"
                    :value="item.key">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="队伍名称">
              <el-input v-model="searchForm.name" placeholder="请输入姓名"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="GetAllTeam">查询</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="rightBar">
        <download-excel
            class="export-excel-wrapper d-inline me-2"
            :data="json_data"
            :fields="json_fields"
            name="竞赛队伍信息表.xls">
          <el-button type="primary" icon="el-icon-download" data-bs-toggle="tooltip" data-bs-placement="top"
                     title="竞赛队伍信息表下载" circle></el-button>
        </download-excel>
        <el-popover
            placement="top"
            width="160"
            v-model="visible">
          <p>确定全部删除吗？</p>
          <div style="text-align: right; margin: 0">
            <el-button size="mini" type="text" @click="visible = false">取消</el-button>
            <el-button type="primary" size="mini" @click="handleClickDel(undefined)">确定</el-button>
          </div>
          <el-button v-permission="['SuperAdmin','Admin']" slot="reference" type="danger" icon="el-icon-delete" data-bs-toggle="tooltip"
                     data-bs-placement="top"
                     title="全部删除" circle></el-button>
        </el-popover>

      </div>
    </div>
    <div class="applyBody">
      <el-table
          ref="singleTable"
          :data="teamData"
          highlight-current-row
          :header-cell-style="{'text-align':'center'}"
          :cell-style="cellStyle"
          border
          style="width: 100%;background-color: white">
        <el-table-column
            type="index"
            label="序号"
            width="50">
        </el-table-column>
        <el-table-column
            property="name"
            label="队伍名称"
            width="120">
        </el-table-column>
        <el-table-column
            property="user[0].name"
            label="队长"
            width="130"
            :show-overflow-tooltip='true'>
        </el-table-column>
        <el-table-column
            label="学院"
            width="130"
            :show-overflow-tooltip='true'>
          <template slot-scope="scope">
            {{ scope.row.user[0].collegeName|college }}
          </template>
        </el-table-column>
        <el-table-column
            label="队伍成员"
            :show-overflow-tooltip='true'
            width="120">
          <template slot-scope="scope">
            {{ scope.row.children|children }}
          </template>
        </el-table-column>
        <el-table-column
            property="contest[0].name"
            label="竞赛名称"
            :show-overflow-tooltip='true'
            width="120">
        </el-table-column>
        <el-table-column
            label="状态"
            width="130">
          <template slot-scope="scope">
            <el-tag effect="dark" :type="scope.row.status === 0?'success':'danger'">
              {{ scope.row.status === 0 ? '可加入' : '不可加入' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
            property="createTime"
            label="创建时间"
            width="130">
        </el-table-column>
        <el-table-column
            property="updateTime"
            label="更新时间"
            width="130">
        </el-table-column>
        <el-table-column
            label="操作">
          <template slot-scope="scope">
            <el-button v-permission="['SuperAdmin','Admin']" type="danger" size="mini" @click="handleClickDel(scope.row._id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          class="mt-3"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[30, 50]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import user from "../../../api/user";
import school from "../../../filter/school";
import college from "../../../filter/college";
import time from "../../../filter/time";
import children from "../../../filter/children";
import init from "../../../api/init";

export default {
  name: "Team",
  mounted() {
    this.GetAllTeam()
  },
  filters: {
    school,
    college,
    time, children
  },
  data() {
    return {
      limit: 5,
      skip: 0,
      page: 1,
      size: 30,
      total: 1,
      teamData: [],
      collegeData: init.college,
      json_fields: {
        "队长": "userName",
        "队名": "name",
        "学院": "collegeName",
        "专业": "majorName",
        "状态": "status",
        "成员": "children",
        "组队时间": "updateTime",
        "竞赛名称": "contestName"
      },
      json_data: [],
      visible: false,
      searchForm: {
        name: '',
        collegeName: this.$store.getters["userStore/userInfo"].collegeName,
      }
    }
  },
  methods: {

    handleSizeChange(size) {
      this.size = size
      this.limit = size
      this.GetAllTeam()
    },
    handleCurrentChange(size) {
      this.page = size
      this.GetAllTeam()
    },
    handleClickDel(id) {
      this.visible = false
      const param = {
        id
      }
      const loading = this.$loading({
        lock: true,
        text: '删除中...请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      user.deleteMyTeam(param).then(r => {
        if (r.data.status === '200') {
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.GetAllTeam()
          loading.close()
        }
      }).catch(() => {
        this.$message.error('删除失败');
        loading.close()
      })
    },

    // 获取全部队伍信息
    GetAllTeam() {
      const param = {
        name: this.searchForm.name,
        collegeName: this.searchForm.collegeName,
        limit: this.limit,
        skip: (this.page - 1) * this.size,
      }
      user.GetAllTeam(param).then(r => {
        let json_data = []
        this.teamData = r.data.data.tm
        // 组excel报表数据
        r.data.data.tm.forEach(v => {
          const obj = {
            userName: v.user[0].name,
            collegeName: college(v.user[0].collegeName),
            status: v.status === 0 ? '可加入' : '不可加入',
            updateTime: time(v.updateTime),
            contestName: v.contest[0].name,
            children: v.children.map(v => v.userName)
          }
          json_data.push(obj)
        })
        this.json_data = json_data
      })
    },


    cellStyle() {
      return {
        'text-align': 'center',
        'overflow': 'hidden',
        'text-overflow': 'ellipsis',
        'white-space': 'nowrap',
      }
    },
  }
}
</script>

<style scoped>

</style>